#include "../mqserver/mq_message.hpp"
#include <gtest/gtest.h>

MQ::MessageManager::ptr msp;


class MesTest : public testing::Environment
{
public:
    void SetUp() override
    {
        LOG(DEBUG, "加载测试环境ing...\n");
        msp = std::make_shared<MQ::MessageManager>("./data/Message");
        msp->InitQueueMessage("queue1");
    }

    void TearDown() override
    {
        LOG(DEBUG, "测试环境清理中ing...\n");
        msp->clear();
    }
};

TEST(MesTest, insert_test)
{
    MQ::BasicProperties bp;
    bp.set_id(UuidHelper::uuid());
    bp.set_routing_key("new.music.#");
    bp.set_delivery_mode(MQ::DeliveryMode::DURABLE);
    msp->insert("queue1", &bp, "Hello world -1", true);
    msp->insert("queue1",nullptr, "Hello world -2", true);
    msp->insert("queue1",nullptr, "Hello world -3", true);
    msp->insert("queue1",nullptr, "Hello world -4", true);
    msp->insert("queue1",nullptr, "Hello world -5", true);

    ASSERT_EQ(msp->total_count("queue1"), 5);
    ASSERT_EQ(msp->durable_count("queue1"), 5);
    ASSERT_EQ(msp->waitack_count("queue1"), 0);
}

// TEST(MesTest, remove_test)
// {
//     MessagePtr msg = msp->front("queue1");
//     ASSERT_EQ(msp->waitack_count("queue1"), 1);
//     msp->ack("queue1", msg->payload().properties().id());
//     ASSERT_EQ(msp->total_count("queue1"), 4);
//     ASSERT_EQ(msp->durable_count("queue1"), 4);
// }

// TEST(MesTest, recovery_test)
// {
//     ASSERT_EQ(msp->total_count("queue1"), 4);
//     ASSERT_EQ(msp->durable_count("queue1"), 4);
//     ASSERT_EQ(msp->waitack_count("queue1"), 0);
// }

// 获取消息测试：获取一条消息，然后在不进行确认的情况下，查看可获取消息数量，待确认消息数量，以及测试消息获取的顺序
TEST(message_test, select_test) 
{
    ASSERT_EQ(msp->durable_count("queue1"), 5);
    MQ::MessagePtr msg1 = msp->front("queue1");
    ASSERT_NE(msg1.get(), nullptr);
    ASSERT_EQ(msg1->payload().body(), std::string("Hello world -1"));
    ASSERT_EQ(msp->durable_count("queue1"), 5);
    ASSERT_EQ(msp->valid_count("queue1"), 4);
    ASSERT_EQ(msp->waitack_count("queue1"), 1);

    MQ::MessagePtr msg2 = msp->front("queue1");
    ASSERT_NE(msg2.get(), nullptr);
    ASSERT_EQ(msg2->payload().body(), std::string("Hello world -2"));
    ASSERT_EQ(msp->durable_count("queue1"), 5);
    ASSERT_EQ(msp->waitack_count("queue1"), 2);

    MQ::MessagePtr msg3 = msp->front("queue1");
    ASSERT_NE(msg3.get(), nullptr);
    ASSERT_EQ(msg3->payload().body(), std::string("Hello world -3"));
    ASSERT_EQ(msp->durable_count("queue1"), 5);
    ASSERT_EQ(msp->waitack_count("queue1"), 3);

    MQ::MessagePtr msg4 = msp->front("queue1");
    ASSERT_NE(msg4.get(), nullptr);
    ASSERT_EQ(msg4->payload().body(), std::string("Hello world -4"));
    ASSERT_EQ(msp->durable_count("queue1"), 5);
    ASSERT_EQ(msp->waitack_count("queue1"), 4);

    MQ::MessagePtr msg5 = msp->front("queue1");
    ASSERT_NE(msg5.get(), nullptr);
    ASSERT_EQ(msg5->payload().body(), std::string("Hello world -5"));
    ASSERT_EQ(msp->durable_count("queue1"), 5);
    ASSERT_EQ(msp->waitack_count("queue1"), 5);
    ASSERT_EQ(msp->valid_count("queue1"), 0);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MesTest);
    RUN_ALL_TESTS();
    return 0;
}